package com.fortune.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.base.BaseController;
import com.fortune.common.paging.PagingData;
import com.fortune.dto.excution.COrderExcution;
import com.fortune.dto.in.cOrder.ClientOrderListByTypeDto;
import com.fortune.enums.COrderEnum;
import com.fortune.service.COrderNewService;
import com.fortune.service.COrderService;
import com.fortune.vo.cOrder.ClientOrder;
import com.fortune.vo.cOrder.DDCOrder;
import com.fortune.vo.cOrder.DDCOrderDetail;
import com.fortune.vo.cOrder.ProductOrderDetails;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "wechat/cOrder", description = "客户订单", tags = { "微信小程序（客户）：订单相关" })
@RestController
@RequestMapping("wechat/cOrder")
@Validated
public class NewCOrderController extends BaseController {
	@Autowired
	private COrderService cOrderService;
	@Autowired
	private COrderNewService cOrderNewService;

	@ApiOperation(value = "获取订单列表(新)", nickname = "clientOrderListNew", notes = "", tags = { "@郭阳" })
	@ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "第几页", paramType = "query", dataType = "int"),
			@ApiImplicitParam(name = "length", value = "每页容量", paramType = "query", dataType = "int"),
			@ApiImplicitParam(name = "type", value = "0未处理，1已批准，2已发货，3已收货，4已完成", paramType = "query", dataType = "int", allowableValues = "0,1,2,3,4"),
			@ApiImplicitParam(name = "Authorization", value = "登录返回的token值", paramType = "header", dataType = "String", required = true) })
	@GetMapping(value = "listNew", produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<ClientOrder>> list(@ApiIgnore @Validated ClientOrderListByTypeDto listDto) {
		int type = listDto.getType();
		String clientId = getUserId();
		List<DDCOrder> findDDCOrderList;
		if (type == 0) {
			findDDCOrderList = cOrderNewService.findDDCOrderList(clientId);
		} else if (type == 1) {
			findDDCOrderList = cOrderNewService.findDDCOrderListByApproved(clientId);
		} else if (type == 2) {
			findDDCOrderList = cOrderNewService.findDDCOrderListByShipped(clientId);
		} else if (type == 3) {
			findDDCOrderList = cOrderNewService.findDDCOrderListByreceived(clientId);
		} else {
			findDDCOrderList = cOrderNewService.findDDCOrderListByCompleted(clientId);
		}
		List<ClientOrder> clientOrders = new ArrayList<>();
		for (DDCOrder ddcOrder : findDDCOrderList) {
			String did = ddcOrder.getdId();
			ClientOrder clientOrder = new ClientOrder();
			// 订单id
			String cOrderId = ddcOrder.getId();
			// 订单编号
			String no = ddcOrder.getNo();
			// 商品总金额
			BigDecimal pTotalPrice = ddcOrder.getpTotalPrice();
			// 押金总金额
			BigDecimal dTotalPrice = ddcOrder.getdTotalPrice();

			/* 查询订单详情 */
			List<DDCOrderDetail> findDDCOrderDetailList = cOrderService.findDDCOrderDetailList(did, clientId, cOrderId);
			/* 返回的订单商品详情 */
			List<ProductOrderDetails> productOrderDetails = new ArrayList<>();
			for (int i = 0; i < findDDCOrderDetailList.size(); i++) {
				DDCOrderDetail ddcOrderDetail = findDDCOrderDetailList.get(i);
				// 厂商简称
				String abbreviation = ddcOrderDetail.getAbbreviation();
				//商品id
				String mpid = ddcOrderDetail.getMpid();
				// 商品名称
				String name = ddcOrderDetail.getName();
				// 商品数量
				float orderNum = ddcOrderDetail.getOrderNum();
				// 商品金额
				float price = ddcOrderDetail.getPrice();
				// 回空箱数
				float backBoxNum = ddcOrderDetail.getBackBoxNum();
				// 押金金额
				BigDecimal dprice = ddcOrderDetail.getDprice();
				boolean cOrderStatus = ddcOrderDetail.iscOrderStatus();
				ProductOrderDetails productOrderDetails1 = new ProductOrderDetails();
				for(int j = 0; j < findDDCOrderDetailList.size(); j++){
					DDCOrderDetail ddcOrderDetai2 = findDDCOrderDetailList.get(j);
					String mpids = ddcOrderDetai2.getMpid();
					boolean cOrderStatus2 = ddcOrderDetai2.iscOrderStatus();
					if(i != j && mpid.equals(mpids) && cOrderStatus == true && cOrderStatus2 == true){ //判断如果遍历的不是是同一条数据 和 商品id相同
						float orderNums = ddcOrderDetai2.getOrderNum();
						// 商品金额
						float prices = ddcOrderDetai2.getPrice();
						// 回空箱数
						float backBoxNums = ddcOrderDetai2.getBackBoxNum();
						float commodityNum = orderNum + orderNums;
						// 商品总金额
						float commodityTotalMoney = price + prices;
						// 回空箱数
						float totalBackBoxNum = backBoxNum + backBoxNums;
						BigDecimal dprices = ddcOrderDetai2.getDprice();
						// 押金金额
						BigDecimal totalDprice = dprice.add(dprices);

						productOrderDetails1.setDid(did);
						productOrderDetails1.setMpid(ddcOrderDetail.getMpid());
						productOrderDetails1.setAbbreviation(abbreviation);
						productOrderDetails1.setName(name);
						productOrderDetails1.setCommodityNum(commodityNum);
						productOrderDetails1.setCommodityTotalMoney(commodityTotalMoney);
						productOrderDetails1.setTotalBackBoxNum(totalBackBoxNum);
						productOrderDetails1.setTotalDprice(totalDprice);
						productOrderDetails.add(productOrderDetails1);


						ddcOrderDetail.setcOrderStatus(false);
						ddcOrderDetai2.setcOrderStatus(false);
						/*else{
							for(ProductOrderDetails productOrderDetails2 : productOrderDetails){
								if(productOrderDetails2.getMpid() != mpids){
									float orderNums = ddcOrderDetai2.getOrderNum();
									// 商品金额
									float prices = ddcOrderDetai2.getPrice();
									// 回空箱数
									float backBoxNums = ddcOrderDetai2.getBackBoxNum();
									float commodityNum = orderNum + orderNums;
									// 商品总金额
									float commodityTotalMoney = price + prices;
									// 回空箱数
									float totalBackBoxNum = backBoxNum + backBoxNums;
									BigDecimal dprices = ddcOrderDetai2.getDprice();
									// 押金金额
									BigDecimal totalDprice = dprice.add(dprices);

									productOrderDetails1.setDid(did);
									productOrderDetails1.setMpid(ddcOrderDetail.getMpid());
									productOrderDetails1.setAbbreviation(abbreviation);
									productOrderDetails1.setName(name);
									productOrderDetails1.setCommodityNum(commodityNum);
									productOrderDetails1.setCommodityTotalMoney(commodityTotalMoney);
									productOrderDetails1.setTotalBackBoxNum(totalBackBoxNum);
									productOrderDetails1.setTotalDprice(totalDprice);
									productOrderDetails.add(productOrderDetails1);
								}
							}
						}*/
					}
				}

			}
			clientOrder.setId(cOrderId);
			clientOrder.setNo(no);
			clientOrder.setdTotalPrice(dTotalPrice);
			clientOrder.setpTotalPrice(pTotalPrice);
			clientOrder.setProductOrderDetails(productOrderDetails);
			clientOrders.add(clientOrder);
		}
		

		PagingData<ClientOrder> pagingData = new PagingData<ClientOrder>();
		pagingData.setData(clientOrders);
		pagingData.setRecordsTotal(findDDCOrderList.size());
		return new COrderExcution<PagingData<ClientOrder>>(pagingData, COrderEnum.FIND_SUCCESS);
	}
}
